package qianfeng.com.meijiadaka;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import qianfeng.com.meijiadaka.adapter.FinishRefresh;
import qianfeng.com.meijiadaka.adapter.OtherAdapter;
import qianfeng.com.meijiadaka.bean.AllBean;
import qianfeng.com.meijiadaka.bean.FindBean;
import qianfeng.com.meijiadaka.interfacebean.FindData;
import qianfeng.com.meijiadaka.myview.MyLiveRecycleView;
import qianfeng.com.meijiadaka.myview.RecycleViewDivider;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class OneActivity extends AppCompatActivity {

    @BindView(R.id.item_title)
    TextView mItemTitle;
    @BindView(R.id.all_rv)
    MyLiveRecycleView mFindRv;
    private List<FindBean.ResultBean> mList;
    private int mPage=1;
    private String mBaseUrl="http://home.naildaka.com/";
    private String mBasePath = "http://home.naildaka.com/vapi/v4/home/picture/style/pictures?styleId=";
    private String mPath = "&uid=null&pageNo=";
    private OtherAdapter mOtherAdapter;
    private AllBean.ResultBean mBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all);
        ButterKnife.bind(this);
        mBean = getIntent().getParcelableExtra("id");
        mItemTitle.setText(mBean.getStyleName());
        initUI();
        loadData();
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl(mBaseUrl).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(FindData.class).
                getData(mBasePath+mBean.getStyleId()+mPath+mPage).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Action1<FindBean>() {
                    @Override
                    public void call(FindBean findBean) {
                        mList.addAll(findBean.getResult());
                        mOtherAdapter.notifyDataSetChanged();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Toast.makeText(OneActivity.this,"亲，网络不好",Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void initUI() {
        mList=new ArrayList<>();
        GridLayoutManager ma=new GridLayoutManager(this,3, LinearLayoutManager.VERTICAL,false);
        mFindRv.getRefreshableView().setLayoutManager(ma);
        mFindRv.getRefreshableView().addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL,3, Color.WHITE));
        mFindRv.getRefreshableView().addItemDecoration(new RecycleViewDivider(this, LinearLayout.VERTICAL,3, Color.WHITE));
        mOtherAdapter = new OtherAdapter(this, mList);
        mFindRv.setAdapter(mOtherAdapter);
        mFindRv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                mPage=1;
                loadData();
                new FinishRefresh(mFindRv,mOtherAdapter).execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {

                mPage++;
                loadData();
                new FinishRefresh(mFindRv,mOtherAdapter).execute();
            }
        });
    }

    @OnClick(R.id.all_ib)
    public void onClick() {
        finish();
    }
}
